﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Web;

/// <summary>
/// ReadExcel 的摘要说明
/// </summary>
public class ReadExcel
{
    private string FileName;
    private string FilsPath;
    private OleDbConnection OleCon = new OleDbConnection();
    private OleDbCommand OleCmd = new OleDbCommand();
    private OleDbDataAdapter OleDa = new OleDbDataAdapter();
    public ReadExcel(string Path)
    {
        this.FileName = Path;
    }
    public ReadExcel(string Path, string Files)
    {
        this.FileName = Path;
        this.FilsPath = Files;
    }
    public DataSet Execl()
    {
        DataSet dataSet = new DataSet();
        this.OleCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + this.FileName + ";Extended Properties=Excel 8.0;";
        this.OleCon.Open();
        this.OleCmd.CommandText = "select * from [Products$]";
        this.OleCmd.Connection = this.OleCon;
        this.OleDa.SelectCommand = this.OleCmd;
        this.OleDa.Fill(dataSet, "Execl");
        this.OleCon.Close();
        this.OleCmd.Dispose();
        this.OleDa.Dispose();
        this.OleCon.Dispose();
        return dataSet;
    }
    public DataSet Csv()
    {
        DataSet dataSet = new DataSet();
        this.OleCon.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + this.FileName + ";Extended Properties='Text;FMT=Delimited;HDR=YES;'";
        this.OleCon.Open();
        this.OleCmd.Connection = this.OleCon;
        this.OleCmd.CommandText = "select * From " + this.FilsPath;
        this.OleDa.SelectCommand = this.OleCmd;
        this.OleDa.Fill(dataSet, "Csv");
        this.OleCon.Close();
        this.OleCmd.Dispose();
        this.OleDa.Dispose();
        this.OleCon.Dispose();
        return dataSet;
    }
}